<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="logout()" type="info">退出登录</el-button>
    </el-header>
    <el-container>
      <!-- 左侧菜单栏 -->
      <el-aside :width="toggle?'64px':'200px'">
        <div class="toggle-btn" @click="toggleMenu()">|||</div>
        <el-menu
          :collapse="toggle"
          :collapse-transition="false"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          router
        >
          <!-- 一级菜单 -->
          <el-submenu :index="v1.id+''" v-for="(v1,i) in menusData" :key="v1.id">
            <template slot="title">
              <i :class="icons[i]"></i>
              <span>{{v1.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="v.path" v-for="v in v1.children" :key="v.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{v.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  data() {
    return {
      // 侧边栏数据
      menusData: [],
      // 导航icon类
      icons: {
        0: 'el-icon-user-solid',
        1: 'el-icon-edit-outline',
        2: 'el-icon-s-goods',
        3: 'el-icon-s-order',
        4: 'el-icon-s-data'
      },
      // 切换侧边栏
      toggle: false
    }
  },
  created() {
    this.getMensData()
  },
  methods: {
    // 切换侧边栏
    toggleMenu() {
      this.toggle = !this.toggle
    },
    // 获取左侧菜单栏数据
    async getMensData() {
      const { data } = await this.$http.get('menus')
      if (data.meta.status !== 200) {
        return this.$message.error('获取左侧菜单栏数据失败,请稍后重试!')
      }
      console.log(data)
      this.menusData = data.data
    },
    // 退出登录
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message.success('退出登录成功!')
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  div {
    display: flex;
    align-items: center;
    span {
      color: #fff;
      font-size: 20px;
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: 0;
  }
  .toggle-btn {
    background-color: #4a5064;
    font-size: 16px;
    color: #fff;
    text-align: center;
    line-height: 24px;
    // 设置字母之间的间隔
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
